package com.yuedong.down.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import java.io.File;

public class UriUtil {
    /**
     * 7.0Uri 机型适配
     * @param context
     * @param file
     * @return
     */
    public static Uri getFileUri(Context context, File file){
        Uri contentUri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0机型 申请临时访问权限
            contentUri = FileProvider.getUriForFile(context, context.getPackageName()+ ".fileprovider", file);
        } else {
            contentUri = Uri.fromFile(file);
        }
        return contentUri;
    }
    private static String getPackageName(Context context,File file) {
        String FilePath=file.getPath();//输入APK地址
        PackageManager pm = context.getPackageManager();
        PackageInfo info = pm.getPackageArchiveInfo(FilePath, PackageManager.GET_ACTIVITIES);
        if(info != null){
            ApplicationInfo appInfo = info.applicationInfo;
            String appName = pm.getApplicationLabel(appInfo).toString();
            String packageName = appInfo.packageName;  //获取安装包名称
          return packageName;

        }
        return context.getPackageName();
    }
}
